package com.ruoyi.system.commons.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.Chatchat;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author QiuYuchun
 * Date: 2023/12/22
 * Time: 16:38
 */

@Service
public class ChatchatGLM3 {

    private final String BASIC_CHAT_GLM3_6B_API = "http://10.1.79.85:7861/chat/chat";


    public String getGLM3Response(String query){
        Chatchat chatchat = new Chatchat();
        chatchat.setQuery(query);
        chatchat.setStream(false);
        chatchat.setMax_tokens(4000);
        chatchat.setModel_name("chatglm3-6b");
        chatchat.setTemperature(0.7);
        chatchat.setPrompt_name("default");
        List<String> ll = new ArrayList<>();
        chatchat.setHistory(ll);
        String requestJson = JSON.toJSONString(chatchat);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setAccept(Arrays.asList(MediaType.TEXT_EVENT_STREAM));
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> requestEntity = new HttpEntity<>(requestJson, httpHeaders);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.exchange(URI.create(BASIC_CHAT_GLM3_6B_API), HttpMethod.POST,requestEntity,String.class);
        String res = responseEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(res);
        String ans = jsonObject.getString("text");
        return ans;
    }



}
